namespace EFS.EventLog.Performance
{
	/// <summary>
	/// Performance measuring by marking events
	/// </summary>
	public class Series<T>
	{
		// multy threading
		private static System.Object sink = new System.Object();

		/// <summary>
		/// Performace
		/// </summary>
		internal Series(System.String seriesName)
		{
			SeriesName = seriesName;
		}

		/// <summary>
		/// Series Name
		/// </summary>
		public System.String SeriesName { get; private set; }

		/// write to file
		public void Write(EFS.EventLog.Performance.Marker<T> marker)
		{
			lock (sink)
			{
				try
				{
					// write
					if (EFS.EventLog.Config.Log.PerformanceLog.Check(SeriesName))
					{
						// open file to write
						System.IO.StreamWriter sw = new System.IO.StreamWriter(FilenameWithDate(SeriesName), true);

						// log process time span
						sw.WriteLine();
						sw.WriteLine(marker.ToString());
						sw.WriteLine("--------------------------------------------------");

						// close
						sw.Close();
					}
				}
				catch (System.Exception e)
				{
					System.Diagnostics.Debug.WriteLine(e);
				}
			}
		}

		/// <summary>
		/// Make file name
		/// </summary>
		/// <param name="fileNameNoExtension"></param>
		/// <returns></returns>
		private static System.String FilenameWithDate(System.String fileNameNoExtension)
		{
			return System.String.Format("{0}{1}-{2}.txt", EFS.EventLog.Config.Log.PerformanceLog.RootDirectory, fileNameNoExtension, System.DateTime.Now.ToString("yyyy-MM-dd"));
		}
	}
}